
//
//  DDCountInputBar.m
//  Dingdong
//
//  Created by 程强 on 16/11/28.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDCountBar.h"

@interface DDCountBar()<UITextFieldDelegate>
@end

@implementation DDCountBar
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.cornerRadius = 3.0;
        self.layer.borderColor = RGBColor(237, 237, 237).CGColor;
        self.layer.borderWidth = 1.0;
        
        weak_Self(self);
        _countTxf = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _countTxf.borderStyle = UITextBorderStyleNone;
        _countTxf.delegate = self;
        _countTxf.keyboardType = UIKeyboardTypeNumberPad;
        _countTxf.textAlignment = NSTextAlignmentCenter;
        _countTxf.text = @"1";
        [self addSubview:_countTxf];
        [_countTxf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf);
            make.left.equalTo(weakSelf).offset(20);
            make.right.equalTo(weakSelf).offset(-20);
            make.height.equalTo(@25);
        }];
        
        UIImageView *reduseIcon = [[UIImageView alloc]init];
        reduseIcon.image = LoadImage(@"details_reduse");
        [self addSubview:reduseIcon];
        [reduseIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf).offset(5);
            make.centerY.equalTo(weakSelf);
            make.width.equalTo(@8);
            make.height.equalTo(@3);
        }];
        
        UIButton *reduseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:reduseBtn];
        [reduseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.centerY.equalTo(weakSelf);
            make.width.height.equalTo(@20);
        }];
        [reduseBtn addTarget:self action:@selector(reduseCount:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:addBtn];
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.centerY.equalTo(weakSelf);
            make.width.height.equalTo(@20);
        }];
        [addBtn addTarget:self action:@selector(addCount:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *addIcon = [[UIImageView alloc]init];
        addIcon.image = LoadImage(@"details_add");
        [self addSubview:addIcon];
        [addIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf).offset(-5);
            make.width.height.equalTo(@12);
            make.centerY.equalTo(weakSelf);
        }];
        
//        //使用NSNotificationCenter 鍵盤出現時
        [[NSNotificationCenter defaultCenter] addObserver:self
         
                                                 selector:@selector(keyboardWasShown:)
         
                                                     name:UIKeyboardDidShowNotification object:nil];
        
        //使用NSNotificationCenter 鍵盤隐藏時
        [[NSNotificationCenter defaultCenter] addObserver:self
         
                                                 selector:@selector(keyboardWillBeHidden:)
         
                                                     name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}
- (void)reduseCount:(UIButton *)btn
{
    if ([self.countTxf.text integerValue]<=1) {
        return;
    }
    self.countTxf.text = [NSString stringWithFormat:@"%ld",[self.countTxf.text integerValue]-1];
}
- (void)addCount:(UIButton *)btn
{
        self.countTxf.text = [NSString stringWithFormat:@"%ld",[self.countTxf.text integerValue]+1];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}
- (void)keyboardWasShown:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    if (self.showBlock) {
        self.showBlock(height);
    }
}
- (void)keyboardWillBeHidden:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    if (self.hiddenBlock) {
        self.hiddenBlock(height);
    }
}
- (void)countBarKeyboardShow:(KeyboardNotificationCallBack)show hiddenKeyboard:(KeyboardNotificationCallBack)hidden
{
    self.showBlock = show;
    self.hiddenBlock = hidden;
}
@end
